<?php
/**
 * Layout Controller for Home page, includes best view product, newest products, category tree... 
 * @author Kota Kakaji
 *
 */
class ControllerCommonLayoutIndex extends Controller {
	/**
	 * Index function
	 */
	protected function index() {
		// Get title of this page.
		$this->data ['title'] = $this->document->title;
		
		// Get description for this page.
		$this->data ['description'] = $this->document->description;
		
		if (isset ( $this->document->home_flg ) == true) {
			$this->data ['home_flg'] = $this->document->home_flg;
		}
		if (! isset ( $this->request->server ['HTTPS'] ) || ($this->request->server ['HTTPS'] != 'on')) {
			$this->data ['base'] = HTTP_SERVER;
		} else {
			$this->data ['base'] = HTTPS_SERVER;
		}
		$this->data ['charset'] = $this->language->get ( 'charset' );
		$this->data ['language'] = $this->language->get ( 'code' );
		$this->data ['direction'] = $this->language->get ( 'direction' );
		$this->data ['links'] = $this->document->links;
		$this->data ['styles'] = $this->document->styles;
		$this->data ['scripts'] = $this->document->scripts;
		$this->data ['breadcrumbs'] = $this->document->breadcrumbs;
		$this->data ['icon'] = $this->config->get ( 'config_icon' );
		$this->data ['contact'] = HTTP_IMAGE . "1002.jpg";
		
		$this->template = $this->config->get ( 'config_template' ) . 'common/layout_index.phtml';
		
		// /Children controller
		$this->children = array (
				'common/header',
				'common/footer' 
		);
		
		$module_data = array ();
		$this->load->model ( 'checkout/extension' );
		$results = $this->model_checkout_extension->getExtensions ( 'module' );
		
		foreach ( $results as $result ) {
			if ($this->config->get ( $result ['key'] . '_status' )) {
				$module_data [] = array (
						'code' => $result ['key'],
						'position' => $this->config->get ( $result ['key'] . '_position' ),
						'sort_order' => $this->config->get ( $result ['key'] . '_sort_order' ) 
				);
			}
			$this->children [] = 'module/' . $result ['key'];
		}
		$sort_order = array ();
		foreach ( $module_data as $key => $value ) {
			$sort_order [$key] = $value ['sort_order'];
		}
		array_multisort ( $sort_order, SORT_ASC, $module_data );
		$this->data ['modules'] = $module_data;
		
		$this->load->model ( 'product_to_catalog/select' );
		
		// Get 6 latest product
		$lastProductArr = $this->model_catalog_product->getLatestProducts ( 6 );
		foreach ( $lastProductArr as $result ) {
			// Load image if have, or default image if product does not have
			// image
			if ($result ['image']) {
				$image = $result ['image'];
			} else {
				$image = 'no_image.jpg';
			}
			// Product data to show in template file
			$this->data ['new_products'] [] = array (
					// Product image
					'thumb' => image_resize ( $image, 120, 120 ),
					
					// Price
					'price' => $this->currency->format ( $this->tax->calculate ( $result ['price'], 9, $this->config->get ( 'config_tax' ) ) ),
					
					// Href to product page
					'href' => $this->model_tool_seo_url->rewrite ( $this->url->http ( 'shops/shop_customer&path=' . '&product_id=' . $result ['product_id'] ) ) 
			);
		}
		// Fetch template file for newest product
		$this->sub_new_product = $this->config->get ( 'config_template' ) . 'common/sub_new_product.phtml';
		$sub_new_pd = $this->fetch ( $this->sub_new_product );
		
		// Store result to data array
		$this->data ['sub_new_product'] = $sub_new_pd;
		
		// Get best view product
		$bestViewProduct = $this->model_catalog_product->getBestViewProduct ( 6, 90 );
		
		if ((is_array ( $bestViewProduct ) == true) && empty ( $bestViewProduct ) == false) {
			foreach ( $bestViewProduct as $result ) {
				if ($result ['image']) {
					$image = $result ['image'];
				} else {
					$image = 'no_image.jpg';
				}
				// Product data to show in template file
				$this->data ['best_view'] [] = array (
						'thumb' => image_resize ( $image, 120, 120 ),
						'price' => $this->currency->format ( $this->tax->calculate ( $result ['price'], 9, $this->config->get ( 'config_tax' ) ) ),
						'href' => $this->model_tool_seo_url->rewrite ( $this->url->http ( 'shops/shop_customer&path=' . '&product_id=' . $result ['product_id'] ) ) 
				);
			}
			
			// Fetch template file for best view product
			$this->sub_bestview_product = $this->config->get ( 'config_template' ) . 'common/sub_bestview_product.phtml';
			$sub_bestview_pd = $this->fetch ( $this->sub_bestview_product );
			
			// Store to data array
			$this->data ['sub_bestview_product'] = $sub_bestview_pd;
		}
		
		// Render controller
		$this->render ();
	}
}
?>